# 线性灰度变换—图像反转

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取原始图像
img = cv2.imread('src/lena.png')

# 图像灰度转换
grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 获取图像高度和宽度
height = grayImage.shape[0]
width = grayImage.shape[1]

# 创建一幅图像
result = np.zeros((height, width), np.uint8)

# 图像灰度反色变换 s=255-r
for i in range(height):
    for j in range(width):
        gray = 255 - grayImage[i, j]
        result[i, j] = np.uint8(gray)

# 显示图像

cv2.imshow("Gray Image", grayImage)
cv2.imshow("Result", result)

# 等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()